<?php
/*
* Author: Nguyen Hai Nam
* Module: Config_CategoryModule
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class Config_GeneralModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function Config_GeneralModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);		
		
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "position"){
			$newscategory 	= new Pick_Dao_NewsCategory();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $newscategory->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();	
		}
		
	}
	public function listAction(){
		
		$category 	= new Pick_Core_Category();
		$actions 	= new Pick_Core_Actions();
		$actions->setInfoAction($category, $this->request);
		
		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		$infoText = '';
		$infoClass = 'infoText';
		
		# Read parameters from request
		$plus 			= $this->request->element('plus','');
		$pid			= $this->request->element('pid','');
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchStatus 	= $this->request->element('searchStatus','-1');
		$page 			= $this->request->element('page',1);
		$orderBy 		= $this->request->element('orderBy','position');
		$orderDir 		= $this->request->element('orderDir','ASC');
		
		# Control plus action
		if($plus){
			$actions->auto($plus, '1=1');
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";		
		}

		# Build search condition
		$defaultName = DEFAULT_LANGUAGE."_name";
		if(!$pid) 
			$searchCond = "cid = 0";
		else
			$searchCond = "cid = $pid"; # For other cases, you can set this to "(1>0)"
		if($searchStatus != '-1') $searchCond .= " AND status = '$searchStatus'";
		if($searchTerms) $searchCond .= " AND ".$defaultName." LIKE '%".$searchTerms."%'";
		
		# Get all category object and pass to template
		$listObjects = $category->getObjects($page, $searchCond, array($orderBy => $orderDir), $items_per_pages);
		
		# Generate status combobox for Search form
		$listStatus = optionStatus($searchStatus);
		
		# Pass some variables to template
		$this->template->assign("pid", $pid);
		$this->template->assign("page", $page);
		$this->template->assign("listStatus", $listStatus);
		$this->template->assign("listObjects", $listObjects);
		$this->template->assign("searchTerms", $searchTerms);
		$this->template->assign("searchStatus", $searchStatus);
		$this->template->assign("orderBy", $orderBy);
		$this->template->assign("orderDir", $orderDir);
		$this->template->assign("infoText", $infoText);
		$this->template->assign("infoClass", $infoClass);
		$this->template->assign("category", $category);
	}
	
	public function addAction(){
		
		$category 		= new Pick_Core_Category();
		$objectInfo 	= new Pick_Core_Categoryinfo();
		$textfilter 	= new Pick_Data_TextFilter();
		$type			= new Pick_Core_Type();
		
		$oId            = $this->request->element('oId','0');
		$plus           = $this->request->element('plus','');
		$infoText 		= '';
		$infoClass 		= '';
		
		# Read parameters from request
		$arrayFieldFromMySQL = $category->getListFields();
		$object = $category->getObject($oId);
		
		if(is_object($object) && $this->request->post('plus')==""){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $object->getField($fieldName);
			}#endforeach		
		}else{
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->post($fieldName,'');
			}#endforeach
		}

		# Modify item of array
		$defaultName = DEFAULT_LANGUAGE."_name";
		$fields['slug'] = $textfilter->urlize($this->request->element('slug', $fields[$defaultName]),false,'-');
		if(is_object($object)){
			$properties		= unserialize($object->getField('properties'));
			$wavatar		= $this->request->element('wavatar', $properties['wavatar']);
			$havatar		= $this->request->element('havatar', $properties['havatar']);
			$wmedium		= $this->request->element('wmedium', $properties['wmedium']);
			$hmedium		= $this->request->element('hmedium', $properties['hmedium']);
			$wbig			= $this->request->element('wbig', $properties['wbig']);
			$hbig			= $this->request->element('hbig', $properties['hbig']);
		}else{
			$wavatar		= $this->request->element('wavatar','');
			$havatar		= $this->request->element('havatar','');
			$wmedium		= $this->request->element('wmedium','');
			$hmedium		= $this->request->element('hmedium','');
			$wbig			= $this->request->element('wbig','');
			$hbig			= $this->request->element('hbig','');
		}
		$properties 	= array(
							'wavatar'	=> 	$wavatar,
							'havatar'	=> 	$havatar,
							'wmedium'	=> 	$wmedium,
							'hmedium'	=> 	$hmedium,
							'wbig'		=> 	$wbig,
							'hbig'		=> 	$hbig	
						);
		$fields['properties'] = serialize($properties);

		# Gan gia tri cho doi tuong				
		$objectInfo->setArrayField($fields);
		$this->template->assign("objectInfo", $objectInfo);

		# Truy du lieu qua template
		$listCategories = $category->getOptionAllCategory(0, $objectInfo->getField('cid'), array('position'=>'ASC'), 'id', $this->lang.'_name', 4);
		$this->template->assign("listCategories",$listCategories);
		
		# combox for status	
		$listStatus = optionStatus($objectInfo->getField('status'),DEFAULT_ADMIN_LANGUAGE);
		$this->template->assign("listStatus",$listStatus);
		
		# combox for type
		$listType = $type->creatOption($objectInfo->getField("typeid"), 'id', $defaultName);
		$this->template->assign("listType",$listType); 	
		
		$this->template->assign('wavatar', $wavatar);
		$this->template->assign('havatar', $havatar);
		$this->template->assign('wmedium', $wmedium);
		$this->template->assign('hmedium', $hmedium);
		$this->template->assign('wbig', $wbig);
		$this->template->assign('hbig', $hbig);	
		
		foreach($arrayFieldFromMySQL as $fieldName){
			$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach
							
		# Begin process data
		if($_POST && $plus == 'saveItem') {
			
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			
			if($validatemess == '') {
				/* Kiem tra neu mau tin da duoc them thi khong cho them nua */
				$object = "";
				if($oId<=0){
							$object = $category->checkDataExits($fields);
				}
				if(is_object($object)){
					$infoText = $this->l('item_inserted_error');	
					$infoClass = "errorText";							
				}else{
					$ok = $category->updateData($objectInfo->getArrayField(),$oId);
					$infoText = $validatemess;
					if($ok){
						$infoText = $this->l('item_save_ok');
						$infoClass = "InfoText";
						$oId = $oId?$oId:mysql_insert_id();			
					}
				}
				
			}else{
				$infoText = $this->l('error').":<br />".$validatemess;
				$infoClass = "errorText";					
			}
		}
		$this->template->assign("oId",$oId);
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);
		$this->template->assign('category', $category);
	}
	
	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validString($this->l('name'), $request->element($this->lang."_name"));		
		return $infoText;
	}
}